﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using System.Transactions;
using T.FAS.Runtime.Base.Service;
using T.FAS.Runtime.Metadata.Entity.Page;
//using T.TTP.TFF.Dao;
//using T.TTP.TFF.Entity;
//using T.TTP.TFF.Service;

namespace T.FAS.Runtime.Page.Dao
{
    public class MenuLogic
    {
        private readonly static MenuDao menuDao = new MenuDao();

        public void AddMenu(FasMenu menu)
        {
            menuDao.Add(menu);
        }

        public void DeleteMenu(string menuId)
        {
            menuDao.Delete(menuId);
        }

        public void UpdateMenu(FasMenu menu)
        {
            menuDao.Update(menu);
        }

        public FasMenu GetMenu(string menuId)
        {
            return menuDao.GetObject(menuId);
        }

        public bool IsExist(string menuId)
        {
            return menuDao.IsExist(menuId);
        }

        public string GetSysApplicationSortName(string applicationId, string subSystemID)
        {
            FunctionTreeService functionTreeService = new FunctionTreeService();
            return functionTreeService.GetApplicationSortName(applicationId, subSystemID);
        }

        public void PublishRouter(FasMenu fasMenu, PageMetaData pageMetaData, string publishType)
        {
            /*
            ///导入WEB框架路由
            string wrpRouterDb = ConfigurationManager.AppSettings["WRPDataBase"];
            TFFFunctionMappingLogic functionMappingLogic = new TFFFunctionMappingLogic();

            if (string.IsNullOrEmpty(wrpRouterDb))
            {
                wrpRouterDb = "TMaster";
            }
            WRPRouterDao routerDao = new WRPRouterDao(wrpRouterDb);
            var functionMappings = functionMappingLogic.GetTFFFunctionMappings(pageMetaData?.Code);

            var instance = TFFClientService.GetInstance();
            switch (publishType)
            {
                case "updateMenu":
                    if (fasMenu == null)
                        throw new Exception();
                    Tools.SyncMenuEntity(fasMenu);
                    string keyAppName = routerDao.GetKeyAppName(fasMenu.KeyApplicationName);
                    string mvcUrlParam = string.Empty;
                    if (fasMenu.MenuUrlParams!=null && fasMenu.MenuUrlParams.Count > 0)
                    {
                        foreach(var menuUrlParam in fasMenu.MenuUrlParams)
                        {
                            if (string.IsNullOrEmpty(menuUrlParam["Key"]) == false) { 
                                mvcUrlParam += menuUrlParam["Key"] + "=" + menuUrlParam["Value"] + "&";
                            }
                        }
                    }
                    if (pageMetaData?.Header?.EngineType == EngineType.GenerateCode)
                    {
                        mvcUrlParam = string.IsNullOrEmpty(mvcUrlParam) ? "" : "?" + mvcUrlParam.TrimEnd('&');
                        routerDao.CreateRouter(keyAppName + ".fas", fasMenu.ID, fasMenu.Url, fasMenu.Name, "gencode/" + fasMenu.ApplicationShortName.ToLower() + "/" + pageMetaData.Code + mvcUrlParam, "", "Fas");
                    }
                    else
                    {
                        mvcUrlParam = string.IsNullOrEmpty(mvcUrlParam) ? "" : "&" + mvcUrlParam.TrimEnd('&');
                        routerDao.CreateRouter(keyAppName + ".fas", fasMenu.ID, fasMenu.Url, fasMenu.Name, "/" + fasMenu.ApplicationShortName.ToLower() + "/render?id=" + fasMenu.PageId+ mvcUrlParam);
                    }
                    break;
                case "deleteMenu":
                    Tools.DeleteMenuEntity(fasMenu.MetaDataID);
                    routerDao.DeleteRouter(fasMenu.Url);
                    break;
                case "updatePage":
                    TFFFunctionMapping functionMapping;
                    if (!string.IsNullOrEmpty(pageMetaData.Header.ApplicationID))
                    {
                        Tools.SyncMenuEntity(pageMetaData);
                        functionMapping = Tools.GetTFFFunctionMapping(pageMetaData, pageMetaData.ID);
                    }
                    else
                    {
                        Tools.DeleteMenuEntity(pageMetaData.ID);
                        functionMapping = Tools.GetTFFFunctionMapping(pageMetaData, "");
                    }
                    instance.DeleteByPageID(pageMetaData.ID);
                    instance.ImportFunctionMapping(functionMapping);//导入功能映射表
                    break;
                case "deletePage":
                    Tools.DeleteMenuEntity(pageMetaData.ID);
                    instance.DeleteByPageID(pageMetaData.ID);
                    break;
            }
            */
        }
    }
}
